<template>
	<view class="box">
		<!-- logo -->
		<view class="login_title">
			欢迎登录小U商城
		</view>
		<image class="login_logo" src="../../static/logo/orange.png" alt="" />
		<button class="login_but" hover-class="touchOpacity" open-type="getPhoneNumber" @getphonenumber="getPhone">
			微信用户一键登录
		</button>
		<view class="login_text" hover-class="touchOpacity" @click="toTextLogin">手机号验证注册/登录</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {}
		},
		onLoad() {},
		methods: {
			toTextLogin() {
				uni.navigateTo({
					url: "/pages/textlogin/textlogin"
				})
			},
			// 微信一键登录
			getPhone(e) {
				// #ifdef MP-WEIXIN
				if (e.detail.errMsg === "getPhoneNumber:fail user deny") {
					uni.showToast({
						title: "用户拒绝授权",
						icon: 'error',
					});
					return
				}
				// 用户允许授权，进行登录
				wx.login({
					success: (res) => {
						const {
							encryptedData,
							iv
						} = e.detail;
						const code = res.code;
						let data = {
							encryptedData,
							iv,
							code
						};

						this.$http({
							url: '/api/wxlogin',
							data,
							method: 'post',
						}).then(res => {
							console.log(res);
							if (res.data.code === 200) {
								// 消息提示
								wx.showToast({
									title: res.data.msg,
								});
								// 数据存储
								wx.setStorage({
									key: 'userInfo',
									data: res.data.list,
								});
								// 页面跳转
								setTimeout(() => {
									wx.reLaunch({
										url: '/pages/user/user'
									})
								}, 1000)
							}
						})
					}
				})
				// #endif
			},


		}
	}
</script>

<style>
	/* 导入外部的样式文件 */
	@import url("@/static/css/login.css");
</style>